package com.jingdianjichi.handler;

import com.jingdianjichi.enums.CallBackTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信回调处理器工厂
 */
@Component
public class CallBackTypeHandlerFactory implements InitializingBean {
    
    @Resource
    private SubscribeHandler subscribeHandler;
    
    @Resource
    private PostCaptchaHandler postCaptchaHandler;
    
    private Map<CallBackTypeEnum, CallBackTypeHandler> handlerMap = new HashMap<>();
    
    /**
     * 根据回调类型获取对应的处理器
     * @param callBackTypeEnum 回调类型枚举
     * @return 对应的处理器
     */
    public CallBackTypeHandler getHandler(CallBackTypeEnum callBackTypeEnum) {
        return handlerMap.get(callBackTypeEnum);
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        handlerMap.put(CallBackTypeEnum.SUBSCRIBE, subscribeHandler);
        handlerMap.put(CallBackTypeEnum.POSTCAPTCHA, postCaptchaHandler);
    }
}
